//
//  SLRecursionController.m
//  Objective-C
//
//  Created by S_Line on 2018/9/4.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLRecursionController.h"

@interface SLRecursionController ()

@property (nonatomic, strong) NSString *text;

@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UILabel *textLabel;

@end

@implementation SLRecursionController

- (void)initNavBar {
    self.navTitle = @"递归算法";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initData];
    
    [self initView];
}

- (void)initData {
//    SLog(@"------->%ld", (long)[self addToNum:10]);
//    SLog(@"------->%ld", (long)[self multiplyToNum:5]);
//    SLog(@"------->%ld", (long)[self fibonacci:10]);
//
//    NSString *str1 = @"abc";
//    NSString *str2 = @"123";
//    SLog(@"str1的地址: %p, str1指向变量的地址: %p, str1指向变量的值: %@, abc的地址: %p", &str1, str1, str1, @"abc");
//    SLog(@"str2的地址: %p, str2指向变量的地址: %p, str2指向变量的值: %@, 123的地址: %p", &str2, str2, str2, @"123");
//    NSString const *strcp = str1;
//    SLog(@"strcp的地址: %p, strcp指向变量的地址: %p, strcp指向变量的值: %@, abc的地址:%p, 123的地址: %p", &strcp, strcp, strcp, @"abc", @"123");
//    strcp = str2;
//    SLog(@"strcp的地址: %p, strcp指向变量的地址: %p, strcp指向变量的值: %@, abc的地址:%p, 123的地址: %p", &strcp, strcp, strcp, @"abc", @"123");
//
//    // 指针常量
//    NSString * const strpc = str2;
//    SLog(@"strpc的地址: %p, strpc指向变量的地址: %p, strpc指向变量的值: %@, abc的地址:%p, 123的地址: %p", &strpc, strpc, strpc, @"abc", @"123");
//    str2 = @"456";
//    SLog(@"strpc的地址: %p, strpc指向变量的地址: %p, strpc指向变量的值: %@, abc的地址:%p, 123的地址: %p, 456的地址%p", &strpc, strpc, strpc, @"abc", @"123", @"456");
}

- (void)initView {
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];
    [scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(kNavigationBarHeight);
        make.bottom.mas_equalTo(kSafeBottomMargin);
    }];
    
    UILabel *textLabel = [[UILabel alloc] init];
    self.textLabel = textLabel;
    [scrollView addSubview:textLabel];
    [textLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(15);
        make.right.bottom.mas_equalTo(-15);
    }];
}

#pragma mark ----- 题目
// 从1加到n
- (NSInteger)addToNum:(NSInteger)number {
    if (number == 0) {
        return 0;
    } else {
        return (number + [self addToNum:number - 1]);
    }
}

// n的阶乘
- (NSInteger)multiplyToNum:(NSInteger)number {
    if (number == 0) {
        return 1;
    } else {
        return (number * [self multiplyToNum:number - 1]);
    }
}

// 斐波那契数列
- (NSInteger)fibonacci:(NSInteger)number {
    if (number == 0) {
        return 0;
    } else if (number == 1) {
        return 1;
    } else {
        return [self fibonacci:number-1] + [self fibonacci: number-2];
    }
}

@end
